#pragma once
#include<iostream>
#include<cstring>
#include<string>
#include<vector>

#define SEP "X"
#define SEP_LEN strlen(SEP)
#define SPACE " "
#define SPACE_LEN strlen(SPACE)

void SpliteMessage(std::string &buffer, std::vector<std::string> *out)
{
    while(true)
    {
        auto pos = buffer.find(SEP);
        if(std::string::npos == pos)
            break;
        std::string message=buffer.substr(0,pos);
        buffer.erase(0,pos+SEP_LEN);
        out->push_back(message);
    }
}
std::string Encode(std::string &s)
{
    return s+SEP;
}
class Request
{
public:
    // 序列化
    //  1  +  1 -> "1 + 1"
    // _x  + _y
    std::string Serialize()
    {
        std::string str;
        str += std::to_string(_x);
        str += SPACE;
        str += _op;
        str += SPACE;
        str += std::to_string(_y);
        return str;
    }
    // 反序列化
    // 1  +  1 <- "1 + 1"
    bool Deserialize(const std::string &str)
    {
        std::size_t left = str.find(SPACE);
        if (left == std::string::npos)
            return false;
        std::size_t right = str.rfind(SPACE);
        if (right == std::string::npos)
            return false;
        _x = atoi(str.substr(0, left).c_str());
        _y = atoi(str.substr(right + SPACE_LEN).c_str());
        if (left + SPACE_LEN > str.size())
            return false;
        else
            _op = str[left + SPACE_LEN];
        return true;
    }

public:
    Request() {}
    Request(int x, int y, char op)
        : _x(x), _y(y), _op(op)
    {
    }
    ~Request() {}

public:
    int _x;
    int _y;
    char _op;
};
// 响应
class Response
{
public:
    // 序列化
    std::string Serialize()
    {
        std::string str;
        str += "code:";
        str += std::to_string(_code);
        str += SPACE;
        str += "result:";
        str += std::to_string(_result);
        return str;
    }
    // 反序列化
    bool Deserialize(const std::string &str)
    {
        std::size_t pos = str.find(SPACE);
        if (pos == std::string::npos)
            return false;
        _code = atoi(str.substr(0, pos).c_str());
        _result = atoi(str.substr(pos + SPACE_LEN).c_str());
        return true;
    }

public:
    Response() {}
    Response(int result, int code, int x, int y, char op)
        : _result(result), _code(code), _x(x), _y(y), _op(op)
    {
    }
    ~Response() {}

public:
    int _result; // 结果
    int _code;   // 错误码

    int _x;
    int _y;
    char _op;
};
